package com.zhangpeng520.attend.service;

import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import com.zhangpeng520.attend.dao.AttendMapper;
import com.zhangpeng520.attend.entity.Attend;
import com.zhangpeng520.common.PageObject.QueryPageBean;
import com.zhangpeng520.common.PageObject.QueryPageCondition;
import com.zhangpeng520.common.date.DateUtils;
import com.zhangpeng520.user.dao.UserMapper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.swing.event.ListDataEvent;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/8/14 0014.
 */

@Service("attendServipml")
public class AttendServipml implements AttendService {

    Log log = LogFactory.getLog(AttendServipml.class);
    final byte ATTEND_NORMAL = 1;
    final byte ATTEND_ABNORMAL = 2;
    final int WORKTIME_NORMAL = 540;

    @SuppressWarnings("SpringJavaAutowiringInspection")
    @Autowired
    private AttendMapper attendMapper;

    @Transactional
    @Override
    public void signAttend(Attend attend) {

        try {
            attendMapper.insertSelective(attend);

        } catch (Exception e) {
            log.error("每日首次打卡错误日志", e);
            throw e;
        }

    }


    /**
     * @Author zhangpeng(www.zhangpeng520.com)
     * @Date 2017/8/15 0015 19:59
     * @Desripition 更新打卡记录
     */

    @Transactional
    @Override
    public void updateAttend(Attend attend) {
        try {
            attendMapper.updateByPrimaryKeySelective(attend);

        } catch (Exception e) {
            log.error("打卡记录更新出错日志", e);
            throw e;
        }
    }


    /**
     * @Author zhangpeng(www.zhangpeng520.com)
     * @Date 2017/8/17 0017 9:11
     * @Desripition
     */

    public Attend findAttend(long userId) {
        Attend attend = attendMapper.selectByUserPrimaryKey(userId);

        return attend;
    }


    /**
     * @Author zhangpeng(www.zhangpeng520.com)
     * @Date 2017/8/17 0017 9:39
     * @Desripition 查找符合条件的记录行数
     */
    public int findAttendRowCount(QueryPageCondition condition) {
        int rowcount = attendMapper.selectAttendRowcount(condition);
        return rowcount;
    }


    /**
     * @Author zhangpeng(www.zhangpeng520.com)
     * @Date 2017/8/17 0017 9:39
     * @Desripition查找符合条件的记录
     */
    @Override
    public QueryPageBean findAttendList(QueryPageCondition condition) {


        QueryPageBean reasult = new QueryPageBean();
        int rowcount = attendMapper.selectAttendRowcount(condition);

        if (rowcount <= 0) {
            return null;
        } else {

            reasult.setTotalRow(rowcount);
            reasult.setCurrentpage(condition.getCurrentpage());
            reasult.setPageSize(condition.getPageSize());
            List<Attend> list = attendMapper.selectAttendList(condition);

            reasult.setAttendlist(list);

        }

        return reasult;
    }


    /**
     * @Author zhangpeng(www.zhangpeng520.com)
     * @Date 2017/8/18 0018 11:07
     * @Desripition定时器检查打卡是否异常
     */
    @Override
    public void checkAttend() {

        List<Attend> attends = attendMapper.selectAllAttend();
        if (attends != null) {
            for (Attend attend : attends) {

                Date morningAttend = attend.getAttendMoring();
                Date eveningAttend = attend.getAttendEvening();
                byte todayWeek=attend.getAttendWeek();
                if (morningAttend != null && eveningAttend != null&&todayWeek!=6&&todayWeek!=7) {

                    if (morningAttend.compareTo(DateUtils.getDate(9, 30)) > 0) {
                        attend.setStatus(ATTEND_ABNORMAL);
                    }
                    if (eveningAttend.compareTo(DateUtils.getDate(18, 30)) < 0) {
                        attend.setStatus(ATTEND_ABNORMAL);
                    }
                    if (DateUtils.getWorkTime(attend.getAttendMoring(), attend.getAttendEvening()) < WORKTIME_NORMAL) {

                        attend.setAbsence(Math.abs(DateUtils.getWorkTime(attend.getAttendMoring(), attend.getAttendEvening()) - WORKTIME_NORMAL));
                        attend.setStatus(ATTEND_ABNORMAL);

                    }
                    if (DateUtils.getWorkTime(attend.getAttendMoring(), attend.getAttendEvening()) > WORKTIME_NORMAL) {
                        attend.setAbsence(0);
                    }
                    System.out.println(DateUtils.getWorkTime(attend.getAttendMoring(), attend.getAttendEvening()));
                    //CollectionUtils.isEmpty(attends);
                    attendMapper.updateByPrimaryKeySelective(attend);
                } else {
                    if (morningAttend == null || eveningAttend == null) {
                        if(todayWeek!=6&&todayWeek!=7) {
                            Date time = new Date();
                            attend.setStatus(ATTEND_ABNORMAL);
                            if (time.compareTo(DateUtils.getDate(23, 50)) > 0 && time.compareTo(DateUtils.getDate(0, 0)) < 0) {
                                attend.setAbsence(WORKTIME_NORMAL);
                            }
                            attendMapper.updateByPrimaryKeySelective(attend);
                        }
                    }
                }
            }
        }



    }


    /**
     *
     *@Author zhangpeng(www.zhangpeng520.com)
     *@Date 2017/8/18 0018 21:49
     *@Desripition 每天晚上定时检查有没有没打卡的员工
     *
     */
    @Override
    public void checkNotAttend() {
        List<Long> list = attendMapper.selectAllnotAttend();
        if(CollectionUtils.isNotEmpty(list)&&DateUtils.getTodayWeek()!=6&&DateUtils.getTodayWeek()!=7) {
            List<Attend> attendList=new ArrayList<>();

            for (Long integer : list) {

                Attend attend = new Attend();
                attend.setUserId(list.get(0));
                attend.setAttendDate(new Date());
                attend.setAbsence(WORKTIME_NORMAL);
                attend.setStatus(ATTEND_ABNORMAL);
                attend.setAttendWeek((byte) DateUtils.getTodayWeek());
                attendList.add(attend);
            }
            attendMapper.batchInsert(attendList);
        }
    }
}
